package cn.hh.pay.controller;


import cn.hh.order.domain.AdoptOrder;
import cn.hh.order.service.IAdoptOrderService;
import cn.hh.pay.config.AlipayConfig;
import cn.hh.pay.constants.OrderConstants;
import cn.hh.pay.domain.AlipayInfo;
import cn.hh.pay.domain.PayAccount;
import cn.hh.pay.domain.PayBill;
import cn.hh.pay.service.IAlipayInfoService;
import cn.hh.pay.service.IPayBillService;
import com.alipay.api.internal.util.AlipaySignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@RestController
public class AlipayController {

    @Autowired
    private IPayBillService payBillService;
    @Autowired
    private IAdoptOrderService adoptOrderService;
    @Autowired
    private IAlipayInfoService alipayInfoService;
    @PostMapping("/notify")
    public void notify(HttpServletRequest request){
        try {
            // 异步回调
            //获取支付宝POST过来反馈信息
            Map<String,String> params = new HashMap<String,String>();
            Map<String,String[]> requestParams = request.getParameterMap();
            for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
                String name = (String) iter.next();
                String[] values = (String[]) requestParams.get(name);
                String valueStr = "";
                for (int i = 0; i < values.length; i++) {
                    valueStr = (i == values.length - 1) ? valueStr + values[i]
                            : valueStr + values[i] + ",";
                }
                //乱码解决，这段代码在出现乱码时使用
                valueStr = new String(valueStr);
                params.put(name, valueStr);
            }
            /*统一支付单号*/
            String unionPaySn = params.get("out_trade_no");
            PayBill bill = payBillService.loadByUnionPaySn(unionPaySn);
            AlipayInfo info = alipayInfoService.getByShopId(bill.getShop_id());
            //调用SDK验证签名
            boolean signVerified = AlipaySignature.rsaCheckV1(params,
                    info.getAlipay_public_key(),
                    AlipayConfig.charset,
                    AlipayConfig.sign_type);
            if(signVerified) {//验证成功
                //商户订单号
                String out_trade_no = unionPaySn;
                //支付宝交易号
                String trade_no = request.getParameter("trade_no");
                //交易状态
                String trade_status = request.getParameter("trade_status");
                if(trade_status.equals("TRADE_FINISHED")){
                    /*不写*/
                }else if (trade_status.equals("TRADE_SUCCESS")){
                    // 1.修改账单状态
                    bill.setState(1);
                    payBillService.update(bill);
                    // 2.修改业务订单状态
                    if (OrderConstants.BUSINESS_TYPE_ADOPT_ORDER.equals(bill.getBusinessKey())){
                        AdoptOrder adoptOrder = adoptOrderService.loadById(bill.getBusinessKey());
                        adoptOrder.setState(1);
                        adoptOrderService.update(adoptOrder);
                        // 3.记录流水
                    }else if(OrderConstants.BUSINESS_TYPE_GOODS_ORDER.equals(bill.getBusinessKey())){

                    }

                }
            }else {//验证失败
                System.out.println("支付失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
